package brac;

import java.util.ArrayList;

import fn.Function;

public class NewtonRaphson extends Bracketing {

	@SuppressWarnings("unchecked")
	public void set() {
		String[] s = { "x", "F(x)", "Error" };
		header = s;
		values = new ArrayList[3];
	}

	public int[] fromTo() {

		return new int[] { 0, 0 };
	}

	public NewtonRaphson() {
		set();
	}

	public void solve(Function fn, double presition, int num_iteration, double p0, double b) {
		ArrayList<Double> pk = new ArrayList<Double>();
		ArrayList<Double> fp = new ArrayList<Double>();
		ArrayList<Double> errorl = new ArrayList<Double>();
		double x0 = p0;
		double x1;
		double f;
		double error;
		for (int i = 0; i < num_iteration; i++) {
			f = feval(fn, x0);
			x1 = x0 - (feval(fn, x0) / feval(fn.derivative(), x0));
			error = Math.abs(x1 - x0);
			errorl.add(error);
			pk.add(x0);
			fp.add(f);
			if (Math.abs(f) <= presition)
				break;
			x0 = x1;
		}
		values[0] = pk;
		values[1] = fp;
		values[2] = errorl;
	}

	@Override
	public String toString() {
		return "Newton-Raphson";
	}

}
